import { BrowserWindow, app } from "electron";
import IpcTemplate from "../core/ipc/ipc-template";
import { distPath } from "@/main/core/project-path";
import { constTemplate } from "@/common/constants";
import path from "node:path"
import { lowdbDefault } from "@/main/core/data/local/kv-datasource"

class LoginWindow extends IpcTemplate {
    private mWindow: BrowserWindow

    constructor() {
        super(constTemplate.loginWindow)
    }

    private createWindow() {
        this.mWindow = new BrowserWindow({
            width: 385,
            height: 660,
            frame: false,
            resizable: false,
            maximizable: false,
            webPreferences: {
                preload: app.isPackaged ?
                    path.join(__dirname, "preload.js") :
                    path.join(distPath, 'preload.js')
            }
        })
    }

    openWindow() {
        if (!this.mWindow) {
            this.createWindow()
        }
        if (app.isPackaged) {
            this.mWindow.loadFile(path.join(__dirname, "index.html"))
        } else {
            this.mWindow.loadURL("http://localhost:3000/user/login")
        }
    }

    minimize() {
        this.mWindow.minimize()
    }

    close() {
        this.mWindow.close()
    }

    onLoginSuccess(){
        //保存登录标记
        lowdbDefault.put("loginStatus",true)
        this.close()

    }

    openDevTools() {
        if (this.mWindow) {
            this.mWindow.webContents.openDevTools({ mode: 'detach' })
        } else {
            console.log("login window is null")
        }
    }

}

const loginWindow = new LoginWindow()

export default loginWindow